#include "head.h"

int insert_house(house A){
    char * sql[250];
    sprintf(sql,"insert into house(floor_name,person_id,house_num,house_type,time)"
                "values('%s',%d,'%s','%s','%s');",A.floor_name,A.person_id,A.house_num,A.house_type,A.time);
    int ls=sqlite3_exec(db,sql,NULL,NULL,NULL);
    if(ls!=SQLITE_OK){
       // fprintf(stderr,"插入失败:%s\n", sqlite3_errmsg(db));
        return -1;
    }
    return 0;
}
void select_house(house A){
    char * sql;
    if(strcmp(A.house_num,"all")==0){
        sql = "select "
              "  h.house_id as 序号,"
              "  floor_name as 楼栋名,"
              "  r.name as 姓名,"
              "  h.house_num as 房间号,"
              "  h.house_type as 房间类型,"
              "  h.time as 装修时间 "
              "  from house h "
              "  left join resident r "
              "  on h.person_id = r.person_id;";
    }else{
        sql="select "
            "  h.house_id as 序号,"
            "  floor_name as 楼栋名,"
            "  r.name as 姓名,"
            "  h.house_num as 房间号,"
            "  h.house_type as 房间类型,"
            "  h.time as 装修时间 "
            "  from house h "
            "  left join resident r "
            "  on h.person_id = r.person_id"
            " where house_num=?;";
    }
    //house_id|	floor_name|	person_id|	house_num|	house_type|	time|
    sqlite3_stmt * stmt;
    sqlite3_prepare_v2(db,sql,-1,&stmt,NULL);
    sqlite3_bind_text(stmt,1,A.house_num,-1,SQLITE_TRANSIENT);
    //查列数
    int house_head_count = sqlite3_column_count(stmt);
    //据算行数
    int a=0;
    while (sqlite3_step(stmt)==SQLITE_ROW){
        a++;
    }
    if(a==0){
        printf("没有该房间！\n");
        sqlite3_finalize(stmt);
        return;
    }
    for (int i = 0; i < house_head_count; i++) {
        printf("%s|\t", sqlite3_column_name(stmt, i));
    }
    printf("\n");
    while (sqlite3_step(stmt)==SQLITE_ROW){
        int house_id = sqlite3_column_int(stmt,0);
        char * floor_name = sqlite3_column_text(stmt,1);
        char *  person_name= sqlite3_column_text(stmt,2);
        char * house_num= sqlite3_column_text(stmt,3);
        char * house_type= sqlite3_column_text(stmt,4);
        char * time= sqlite3_column_text(stmt,5);
        printf("|%d\t|\t%s\t|\t%s\t|\t%s|\t%s|\t%s\n",house_id,floor_name,person_name,house_num,house_type,time);
    }
    //释放语句句柄
    sqlite3_finalize(stmt);
}
//根据房间号删除房间
int delete_house(char * house_num){
    sqlite3_stmt * stmt;
    char * sql1[250];
    sprintf(sql1,"select house_num from house where house_num='%s';",house_num);
    sqlite3_prepare_v2(db,sql1,-1,&stmt,NULL);
    int a=0;
    while (sqlite3_step(stmt)==SQLITE_ROW){
        a++;
    }
    if(a==0){
        printf("没有该房间，请重新输入:\n");
        sqlite3_finalize(stmt);
        return -1;
    }
    char sql[250];
    sprintf(sql,"delete from house where house_num= '%s';",house_num);
    sqlite3_exec(db,sql,NULL,NULL,NULL);
    printf("删除成功!\n");
    sqlite3_finalize(stmt);
    return 0;
}
void house_main(){
    house A;
    int d;//判断是否成功
    int a,b=1,c=0;
    printf("1.增加房屋 2.查询房屋 3.更改房屋信息 4.删除房屋 5.返回\n");
    a= hcq(a);
    while(b){
        if(c!=0){
            printf("1.增加房屋 2.查询房屋 3.更改房屋信息 4.删除房屋 5.返回\n");
            a= hcq(a);
        }c++;
        switch (a) {
            case 1:
                printf("----请输楼栋名称,住户编号,房间号,房间类型,入住日期----\n");
                while(1){
                    scanf("%s",A.floor_name);
                    scanf("%d",&A.person_id);
                    scanf("%s",A.house_num);
                    scanf("%s",A.house_type);
                    scanf("%s",A.time);
                    d= insert_house(A);
                    if(d==-1){
                        printf("房间号已存在请重新输入:\n");
                        continue;
                    }else{
                        printf("添加成功\n");
                        break;
                    }
                }
                break;
            case 2:
                printf("输入\'all\'查全部，输入\'房间号\'指定查询\n");
                scanf("%s",A.house_num);
                select_house(A);
                break;
            case 3:
                printf("请输入您要更改的房间号名称:\n");
                while (1){
                    scanf("%s",A.house_num);
                    d= judge_house(A.house_num);
                    if(d==-1){
                        continue;
                    } else{
                        update_house(A.house_num);
                        break;
                    }
                }
                break;
            case 4:
                while (1){
                    printf("请输入要删除的房屋号\n:");
                    scanf("%s",A.house_num);
                    d= judge_house(A.house_num);
                    if(d==-1){
                        continue;
                    } else{
                        delete_house(A.house_num);
                        break;
                    }
                }
                break;
            case 5:
                b=0;
                break;
            default:
                printf("输入错误请重新输入:");
                break;
        }
    }
}
//用于判断房间是否存在
int judge_house(char *house_num){
    int a=0;
    sqlite3_stmt * stmt;
    char sql1[250];
    sprintf(sql1,"select house_num from house where house_num='%s';",house_num);
    sqlite3_prepare_v2(db,sql1,-1,&stmt,NULL);
    while (sqlite3_step(stmt)==SQLITE_ROW){
        a++;
    }
    sqlite3_finalize(stmt);
    if(a==0){
        printf("没有该房间号，请重新输入:\n");
        return -1;
    }
    return 0;
}
//用于更改信息的二次导航
void update_house(char * house_num){
    int a;//用于导航选择
    int b=1;//循环条件
    int c=0;//避免重复提醒
    char d[50];//存放新改的内容
    int e;//存放避免重复楼栋
    int f;//用于存放更改int类型数据
    printf("----------------请选择要更改:%s的什么内容----------------\n",house_num);
    printf(" 1.更改人员编号 2.更改房间号 3.更改房间类型 4.更改入户时间  5.退出\n");
    a= hcq(a);
    while(b){
        if(c!=0){
            printf("----------------请选择要更改:%s的什么内容----------------\n",house_num);
            printf(" 1.更改人员编号 2.更改房间号 3.更改房间类型 4.更改入户时间  5.退出\n");
            a= hcq(a);
        }
        c++;
        switch (a) {
            case 1:
                printf("请输入新人员编号！\n");
                scanf("%d",&f);
                e=update_house3(house_num,f,"person_id");
                if(e!=-1){
                    printf("更改成功！\n");
                }
                break;
            case 2:
                printf("请输入新房间号！\n");
                while (1){
                    scanf("%s",d);
                    e=update_house2(house_num,d,"house_num");
                    if(e!=-1){
                        printf("更改成功！\n");
                        break;
                    }
                    printf("房间号已存在，请重新输入:");
                }
                break;
            case 3:
                printf("请输入新房间类型！\n");
                scanf("%s",d);
                e=update_house2(house_num,d,"house_type");
                if(e!=-1){
                    printf("更改成功！\n");
                }
                break;
            case 4:
                printf("请输入新入户日期！\n");
                scanf("%s",d);
                e=update_house2(house_num,d,"house_type");
                if(e!=-1){
                    printf("更改成功！\n");
                }
                break;
            case 5:
                b=0;
                break;
            default:
                printf("输入错误请重新输入:\n");
                break;
        }
    }
}
int update_house2(char *house_num,char * new,char * head) {
    char sql[250];
    sprintf(sql, "update house set %s='%s' where house_num='%s';", head, new, house_num);
    int ls = sqlite3_exec(db, sql, NULL, NULL, NULL);
    if (ls != SQLITE_OK) {
        return -1;
    }
    return 0;
}
int update_house3(char *house_num,int new,char * head){
    char sql[250];
    sprintf(sql,"update house set %s=%d where house_num='%s';",head,new,house_num);
    int ls=sqlite3_exec(db,sql,NULL,NULL,NULL);
    if (ls!=SQLITE_OK){
        return -1;
    }
    return 0;
}